package br.inf.ufrgs.usixml4cdc.logicconector.java;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

public class DomainModel {
	
	private static DomainModel _instance = null;
	

	private URLClassLoader classLoader;
		
	private DomainModel(){
		try {
			//TODO: Make this URL load the entire directory
			URL url = new URL("file:/D:/frk/Projetos Software/Calc/Calc_CDC/dist/Calc_CDC.jar");
			this.classLoader = URLClassLoader.newInstance(
					new URL[]{url}, this.getClass().getClassLoader());
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		
	}
	
	public static DomainModel getInstance(){
		if (_instance == null){
			_instance = new DomainModel();
		}
		
		return _instance;
	}
	
	private Object currentObject = null;
	
	public Object getDomainModelObject(String className){
		
		if (this.currentObject == null){
			Class currentClass;
			Constructor ct;
			try {
				currentClass = this.classLoader.loadClass(className);
				ct = currentClass.getConstructor(new Class[]{});
				this.currentObject = ct.newInstance(new Object[0]);
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			
		}
		
		return currentObject;
		
	}

}
